/*
 * File :  RCBaseFragment.java
 * Description : RCBaseFragment
 * Author :  王建
 * Time :  16-9-26 下午5:12
 *
 * Copyright (C) 2016 Chengdu Ruichuangzhibo Technology Co., Ltd. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wman.sheep.mvc.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.wman.sheep.R;

import java.lang.ref.WeakReference;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


/**
 * 碎片基类
 */
public abstract class BaseMVCFragment extends Fragment implements
        View.OnClickListener, BaseFragmentInterface {

    /**
     * 实例化一个UiHandler处理
     */
    protected Handler mUiHandler = new UiHandler(this) {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (getFragmentReference() != null && getFragmentReference().get() != null) {
                try {
                    getFragmentReference().get().handleUiMessage(msg);
                } catch (IllegalStateException e) {
                }
            }
        }

        ;
    };

    /**
     * 自定义UiHandler,用于处理其他线程发送的消息
     *
     * @author dane55
     */
    private static class UiHandler extends Handler {
        // 定义RCBaseFragment的弱引用，以保证虚拟机随时可以回收此页面
        private final WeakReference<BaseMVCFragment> mFragmentReference;

        /**
         * 构建自定义的UIHandler，传递当前页面引用(Activity或者BaseFragment)
         *
         * @param activity
         */
        public UiHandler(BaseMVCFragment activity) {
            mFragmentReference = new WeakReference<BaseMVCFragment>(activity);
        }

        /**
         * 获取当前页面(Activity或者BaseFragment)
         *
         * @return
         */
        public WeakReference<BaseMVCFragment> getFragmentReference() {
            return mFragmentReference;
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutID(), container, false);

//        ButterKnife.bind(view);

        initView(view);

        initData();

        return view;
    }

    /**
     * 绑定当前View所使用的布局
     *
     * @return layout布局ID
     */
    protected abstract int getLayoutID();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 处理更新UI任务(派生类需要重写此方法，用于处理自身业务)
     *
     * @param msg
     */
    protected void handleUiMessage(Message msg) {
    }

    /**
     * 发送UI更新操作
     *
     * @param msg
     */
    protected void sendUiMessage(Message msg) {
        mUiHandler.sendMessage(msg);
    }


    /**
     * 发送UI更新操作
     *
     * @param what
     */
    protected void sendEmptyUiMessage(int what) {
        mUiHandler.sendEmptyMessage(what);
    }

    /**
     * 隐藏软键盘
     */
    protected void hideSoftInput(Context context) {
        InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // manager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        if (getActivity().getCurrentFocus() != null) {
            manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示软键盘
     */
    protected void showSoftInput() {
        InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }

    /**
     * 接收来自BroadcastReceiver数据集
     *
     * @param context
     * @param intent
     */
    public void onReceive(Context context, Intent intent) {

    }

    @Override
    public void onClick(View v) {

    }

    /**
     * @param it
     * @return void
     * @Title: startAnimationActivity
     * @Description: 开始动画跳转
     */
    public void startAnimationActivity(Intent it) {
        getActivity().startActivity(it);
        getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_of_left);
    }

    /**
     * @param it
     * @param requestCode
     * @return void
     * @Title: startAnimationActivityForResult
     * @Description: 开始动画跳转
     */
    public void startAnimationActivityForResult(Intent it, int requestCode) {
        getActivity().startActivityForResult(it, requestCode);
        getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_of_left);
    }

    /**
     * @return void
     * @Title: finishAnimationActivity
     * @Description: 动画方式结束页面
     */
    public void finishAnimationActivity() {
        getActivity().finish();
        getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_of_right);
    }
}